package com.cx.mall.common.filter;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CacheBloomFilter<T> {
    @Value("${cache.bloom.name}")
    private String name;
    @Value("${cache.bloom.expectedInsertions}")
    private Long expectedInsertions;
    @Value("${cache.bloom.falseProbability}")
    private Double falseProbability;
    /**
     * 初始化布隆过滤器
     */
    public RBloomFilter getBloomFilter(RedissonClient redissonClient){
        RBloomFilter<T> bloomFilter = redissonClient.getBloomFilter(name);
        bloomFilter.tryInit(expectedInsertions,falseProbability);
        return bloomFilter;
    }
    /**
     * 判断布隆过滤器是否存在key
     */
    public boolean contains(RBloomFilter<Object> bloomFilter,Object value){
        return bloomFilter.contains(value);
    }
}
